package com.dao;

import com.po.User;
import com.utils.JDBCUnits;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdministratorDao {
    /**
     * 获取冻结用户的名单
     * @return
     */
    public static List<User> getFrozenUser(){
        List<User> frozenUsers = new ArrayList<>();
        Connection c = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            c = JDBCUnits.getConnectino();
            String sql = "select id ,username ,name , create_time from t_usr  where status = '冻结'";
            ps = c.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                User user = new User();
                user.setTime(rs.getDate("create_time"));
                user.setId(rs.getInt("id"));
                user.setName(rs.getString("name"));
                user.setUsername(rs.getString("username"));
                frozenUsers.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUnits.close(c,ps,rs);
        }

        return frozenUsers;
    }

    /**
     * 系统管理员解冻
     * @param id
     */
    public static void relieveFrozen(int id){
        Connection c = null;
        PreparedStatement ps = null;
        try {
            c = JDBCUnits.getConnectino();
            String sql = "update t_usr set status ='1' where id =?";
            ps = c.prepareStatement(sql);
            ps.setInt(1,id);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUnits.close(c,ps);
        }
    }

    /**
     * 系统管理员冻结某个用户
     * @param id
     */
    public static void setFreezing(int id){
        Connection c = null;
        PreparedStatement ps = null;
        try {
            c = JDBCUnits.getConnectino();
            String sql = "update t_usr set status = '冻结' where id =?";
            ps = c.prepareStatement(sql);
            ps.setInt(1,NoteDao.getUserId(id));
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUnits.close(c, ps);
        }
    }

    /**
     * 获取冻结用户的名单
     * @param users 冻结用户的集合
     * @return 返回一个集合
     */
    public static List<Integer> getFrozenId(List<User> users){
        List<Integer> list = new ArrayList<>();
        for(User user : users){
            list.add(user.getId());
        }

        return list;
    }
}
